package edu.sctu.yueding.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;

import static com.tencent.open.utils.Global.getPackageName;
import static com.umeng.socialize.Config.dialog;

/**
 * Created by wfx on 16-9-21.
 */

public class PermissionUtil {

    public static int PERMISSION_TAG = 123;

    // 提示用户该请求权限的弹出框
    public static void showDialogTipUserRequestPermission(final Activity mActivity, final String[] permissions) {

        new AlertDialog.Builder(mActivity)
                .setTitle("存储权限不可用")
                .setMessage("由于支付宝需要获取存储空间，为你存储个人信息；\n否则，您将无法正常使用支付宝")
                .setPositiveButton("立即开启", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        startRequestPermission(mActivity, permissions);
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).setCancelable(false).show();
    }

    // 开始提交请求权限
    private static void startRequestPermission(Activity mActivity, String[] permissions) {
        ActivityCompat.requestPermissions(mActivity, permissions, 321);
    }

    // 提示用户去应用设置界面手动开启权限

    public static void showDialogTipUserGoToAppSettting(final Activity mActivity) {

        dialog = new AlertDialog.Builder(mActivity)
                .setTitle("存储权限不可用")
                .setMessage("请在-应用设置-权限-中，允许悦定使用存储权限来读取用户数据")
                .setPositiveButton("立即开启", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 跳转到应用设置界面
                        goToAppSetting(mActivity);
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mActivity.finish();
                    }
                }).setCancelable(false).show();
    }

    // 跳转到当前应用的设置界面
    private static void goToAppSetting(Activity mActivity) {
        Intent intent = new Intent();

        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        Uri uri = Uri.fromParts("package", getPackageName(), null);
        intent.setData(uri);

        mActivity.startActivityForResult(intent, PERMISSION_TAG);
    }



}
